/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dp1.titandevelop.titano.view.prod;


import dp1.titandevelop.titano.bean.ProduccionAux;
import dp1.titandevelop.titano.persistent.PadreProduccion;
import dp1.titandevelop.titano.service.ProduccionService;
import java.awt.Dimension;

import java.text.DateFormat;
import java.util.ArrayList;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author Dorita
 */
// Modelo de tabla
public class ModulosT_PRO_AdmPro extends javax.swing.JInternalFrame {

    ArrayList<PadreProduccion> listaPadreProduccion;
    ProduccionService s= new ProduccionService();
    
    
    
    //Refrescar la tabla de productos
    public void RefrescarTabla() {
        AbstractTableModel aux = (AbstractTableModel) this.tblReceta.getModel();
        aux.fireTableDataChanged();
    }
    
     
         
    private AbstractTableModel modelo = new AbstractTableModel() { // usando una clase anonima
        // colocando los titulos de las columnas

        String[] titles = {"Fecha", "Beneficio","Costo"};
        //String[] titles = {"Fecha","Descripcion", "Beneficio","Costo"};
        public String getColumnName(int col) {
            return titles[col];
        }

        public int getRowCount() {
            if (listaPadreProduccion!=null)
            return listaPadreProduccion.size();
            else return 0;// cuantos registros (filas)
        }

        public int getColumnCount() {
            //return 4
            return 3; // cuantas columnas
        }

        public Object getValueAt(int rowIndex, int columnIndex) {
            
            PadreProduccion p = listaPadreProduccion.get(rowIndex);
            DateFormat fecha = DateFormat.getDateInstance(DateFormat.MEDIUM);
            ArrayList<ProduccionAux> pa=s.buscaProductos(p.getIdpadreproduccion());
            float beneficio =0, costo=0;
            for (int i=0; i<pa.size(); i++)
            {
                beneficio+=pa.get(i).getBeneficio();
                costo+=pa.get(i).getCosto();
            }
            switch (columnIndex) {
                 case 0:
                    return fecha.format(p.getFecha());
//               case 1: 
//                  return descripcion;
//               case 2: 
//                  return beneficio;
//               case 3:
//                  return costo;  
                 case 1: 
                     return "S./"+beneficio;
                 case 2:
                    return "S./" +costo;                        
        }
            return null;
        }

        public PadreProduccion getValue(int row) {
            return listaPadreProduccion.get(row);
        }
        
    };

    /**
     * Creates new form TitanoInteralSegADMUser
     */
    public ModulosT_PRO_AdmPro() {
        initComponents();
        listaPadreProduccion = new ArrayList<PadreProduccion>();       
        this.tblReceta.setModel(this.modelo); 
        
    }
    
      
     /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        txtFecha = new org.jdesktop.swingx.JXDatePicker();
        jLabel1 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblReceta = new javax.swing.JTable();
        botonBuscar = new javax.swing.JButton();
        botonNuevo = new javax.swing.JButton();
        botonLimpiar = new javax.swing.JButton();
        botonSalir = new javax.swing.JButton();
        btnEditar = new javax.swing.JButton();

        setResizable(true);
        setTitle("Administrar Producción");
        setMaximumSize(new java.awt.Dimension(650, 450));
        setMinimumSize(new java.awt.Dimension(650, 450));
        setName(""); // NOI18N
        setPreferredSize(new java.awt.Dimension(650, 450));

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Filtros de Búsqueda"));
        jPanel1.setFont(new java.awt.Font("Tekton Pro", 0, 11)); // NOI18N

        jLabel1.setText("Fecha");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(33, 33, 33)
                .addComponent(txtFecha, javax.swing.GroupLayout.PREFERRED_SIZE, 156, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(352, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtFecha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(0, 8, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Resultado de Búsqueda"));
        jPanel2.setFont(new java.awt.Font("Tekton Pro", 0, 11)); // NOI18N

        tblReceta.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Producto"
            }
        ));
        jScrollPane1.setViewportView(tblReceta);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 560, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 166, Short.MAX_VALUE)
                .addContainerGap())
        );

        botonBuscar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/buscar.png"))); // NOI18N
        botonBuscar.setToolTipText("Buscar");
        botonBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonBuscarActionPerformed(evt);
            }
        });

        botonNuevo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/nuevo.png"))); // NOI18N
        botonNuevo.setToolTipText("Nuevo");
        botonNuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonNuevoActionPerformed(evt);
            }
        });

        botonLimpiar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/limpiar.png"))); // NOI18N
        botonLimpiar.setToolTipText("Limpiar");
        botonLimpiar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonLimpiarActionPerformed(evt);
            }
        });

        botonSalir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/retroceder.png"))); // NOI18N
        botonSalir.setToolTipText("Sali");
        botonSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonSalirActionPerformed(evt);
            }
        });

        btnEditar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/detalle.png"))); // NOI18N
        btnEditar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(8, 8, 8)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(botonBuscar)
                        .addGap(1, 1, 1)
                        .addComponent(botonNuevo)
                        .addGap(1, 1, 1)
                        .addComponent(botonLimpiar)
                        .addGap(1, 1, 1)
                        .addComponent(botonSalir))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(btnEditar)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(8, 8, 8))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(8, 8, 8)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(botonBuscar)
                            .addComponent(botonNuevo, javax.swing.GroupLayout.Alignment.TRAILING))
                        .addComponent(botonLimpiar, javax.swing.GroupLayout.Alignment.TRAILING))
                    .addComponent(botonSalir))
                .addGap(8, 8, 8)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(8, 8, 8)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(8, 8, 8)
                .addComponent(btnEditar)
                .addGap(8, 8, 8))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void botonNuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonNuevoActionPerformed
        
        
        ModulosT_PRO.ventanaProduccionNuevo = new ModulosT_PRO_ProduccionNuevo();
        this.getParent().add(ModulosT_PRO.ventanaProduccionNuevo);

        Dimension desktopSize = this.getParent().getSize();
        Dimension jInternalFrameSize = ModulosT_PRO.ventanaProduccionNuevo.getSize();
        ModulosT_PRO.ventanaProduccionNuevo.setLocation((desktopSize.width - jInternalFrameSize.width) / 2,
            (desktopSize.height - jInternalFrameSize.height)/4 );
        ModulosT_PRO.ventanaProduccionNuevo.show();

        this.dispose();
    }//GEN-LAST:event_botonNuevoActionPerformed

    private void botonLimpiarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonLimpiarActionPerformed
        // TODO add your handling code here:
       
        txtFecha.setDate(null);
        listaPadreProduccion=null;
        this.RefrescarTabla();
    }//GEN-LAST:event_botonLimpiarActionPerformed

    private void botonSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonSalirActionPerformed
        // TODO add your handling code here:
        ModulosT_PRO.admProduccion=null;
        this.dispose();
    }//GEN-LAST:event_botonSalirActionPerformed

    private void botonBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonBuscarActionPerformed
        // TODO add your handling code here:
        if(txtFecha.getDate()==null) listaPadreProduccion=s.BuscarTodos();
        else this.listaPadreProduccion=s.BuscaPorFecha(txtFecha.getDate());
        this.RefrescarTabla();
    }//GEN-LAST:event_botonBuscarActionPerformed

    private void btnEditarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditarActionPerformed
        // TODO add your handling code here:
        ModulosT_PRO.ventanaProduccionEdita = new ModulosT_PRO_ProduccionEdita(this.listaPadreProduccion.get(this.tblReceta.getSelectedRow()));
        this.getParent().add(ModulosT_PRO.ventanaProduccionEdita);

        Dimension desktopSize = this.getParent().getSize();
        Dimension jInternalFrameSize = ModulosT_PRO.ventanaProduccionEdita.getSize();
        ModulosT_PRO.ventanaProduccionEdita.setLocation((desktopSize.width - jInternalFrameSize.width) / 2,
            (desktopSize.height - jInternalFrameSize.height)/4 );
        ModulosT_PRO.ventanaProduccionEdita.show();
         ModulosT_PRO.admProduccion=null;
        this.dispose();
    }//GEN-LAST:event_btnEditarActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botonBuscar;
    private javax.swing.JButton botonLimpiar;
    private javax.swing.JButton botonNuevo;
    private javax.swing.JButton botonSalir;
    private javax.swing.JButton btnEditar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblReceta;
    private org.jdesktop.swingx.JXDatePicker txtFecha;
    // End of variables declaration//GEN-END:variables

   
}
